package view2.utils;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.NewsListAdapter;
import com.example.zhinongbao.entity.NewsInfo;
import com.example.zhinongbao.view.NewsDetailsActivity;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class TabNewsFragment extends Fragment {
    private String url="http://v.juhe.cn/toutiao/index?key=e69ede0a62b5bb1bbacc9fe4529fe19a&type=";

    private static final String ARG_PARAM = "title";
    private  View rootView;

    private  String title;

    private RecyclerView  recyclerView;

    private NewsListAdapter mNewsListAdapter;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                String data = (String) msg.obj;
                NewsInfo newsInfo = new Gson().fromJson(data, NewsInfo.class);
                //刷新适配器
                if (null!=mNewsListAdapter){
                    mNewsListAdapter.setListData(newsInfo.getResult().getData());
                }
            }
        }
    };

    public TabNewsFragment() {
        // Required empty public constructor
    }


    public static TabNewsFragment newInstance(String param ) {
        TabNewsFragment fragment = new TabNewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM,  param);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_PARAM);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView=inflater.inflate(R.layout.fragment_tab_news, container, false);
        recyclerView=rootView.findViewById(R.id.recyclerView);
        return  rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化适配器
        mNewsListAdapter=new NewsListAdapter(getActivity());
        //设置adapter
        recyclerView.setAdapter(mNewsListAdapter);
        //recycleView列表点击事件
        mNewsListAdapter.setOnItemClickListener(new NewsListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(NewsInfo.ResultDTO.DataDTO dataDTO, int position) {
                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
                intent.putExtra("dataDTO",dataDTO);
                startActivity(intent);
            }
        });
        //获取数据
        getHttpData();
    }

    private void getHttpData() {

        //创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //构构造Request对象
        Request request = new Request.Builder()
                .url(url+title)
                .get()
                .build()
                ;

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.d("-------------","onFailure:"+e.toString());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String data= response.body().string();
//                Log.d("-------------","onResponse:"+data);
//                NewsInfo newsInfo=new Gson().fromJson(data,NewsInfo.class);
//                if(newsInfo!=null&&newsInfo.getError_code()==0){
//                    //逻辑处理
//                    if (null!=mNewsListAdapter) {
//                        mNewsListAdapter.setListData(newsInfo.getResult().getData());
//
//
//                    }
//                }
//               else{
//                    Toast.makeText(getActivity(),"获取数据失败，请稍后重试",Toast.LENGTH_SHORT).show();
//                }
                Message message=new Message();
                message.what = 100;
                message.obj = data;
                mHandler.sendMessage(message);
            }
        });


    }

}
